#!/bin/bash
# 项目打包脚本

echo "===================================="
echo "Apple固件监控系统 - 打包脚本"
echo "===================================="

# 设置变量
PROJECT_NAME="iphone-firmware-monitor"
VERSION="2.1"
DATE=$(date +%Y%m%d)
PACKAGE_NAME="${PROJECT_NAME}_v${VERSION}_${DATE}.tar.gz"

echo ""
echo "打包信息:"
echo "  项目: $PROJECT_NAME"
echo "  版本: $VERSION"
echo "  日期: $DATE"
echo "  文件: $PACKAGE_NAME"
echo ""

# 创建临时目录
TMP_DIR="/tmp/${PROJECT_NAME}_build"
rm -rf $TMP_DIR
mkdir -p $TMP_DIR

echo "[1/5] 复制项目文件..."

# 复制必要文件
cp firmware_monitor.py $TMP_DIR/
cp requirements.txt $TMP_DIR/
cp README.md $TMP_DIR/
cp CHANGELOG.md $TMP_DIR/
cp -r config $TMP_DIR/
cp -r docs $TMP_DIR/
cp -r scripts $TMP_DIR/

# 创建必要的目录
mkdir -p $TMP_DIR/logs

echo "[2/5] 排除不必要文件..."

# 清理临时文件
find $TMP_DIR -name "*.pyc" -delete
find $TMP_DIR -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null
rm -f $TMP_DIR/.env
rm -f $TMP_DIR/firmware_status.json

echo "[3/5] 创建部署说明..."

# 创建部署说明文件
cat > $TMP_DIR/DEPLOY.txt << 'EOF'
Apple固件监控系统 - 部署说明
==================================

1. 上传到服务器
   scp iphone-firmware-monitor_*.tar.gz user@server:~/

2. 解压
   tar -xzf iphone-firmware-monitor_*.tar.gz
   cd iphone-firmware-monitor

3. 安装环境
   python3 -m venv venv
   source venv/bin/activate
   pip install -r requirements.txt

4. 配置
   cp config/config.example.env .env
   nano .env

5. 测试运行
   python firmware_monitor.py --once

6. 配置服务
   详见 docs/DEPLOYMENT_GUIDE.md

更多信息请查看：
- README.md - 项目说明
- docs/DEPLOYMENT_GUIDE.md - 详细部署指南
- CHANGELOG.md - 更新日志

联系：GitHub Issues
EOF

echo "[4/5] 打包压缩..."

# 打包
cd /tmp
tar -czf $PACKAGE_NAME -C $TMP_DIR .

# 移动到当前目录
mv $PACKAGE_NAME "$(pwd)"

echo "[5/5] 清理临时文件..."
rm -rf $TMP_DIR

echo ""
echo "===================================="
echo "打包完成！"
echo "===================================="
echo ""
echo "文件位置: $(pwd)/$PACKAGE_NAME"
echo ""
ls -lh $PACKAGE_NAME
echo ""
echo "下一步:"
echo "  1. 上传到服务器"
echo "     scp $PACKAGE_NAME user@server:~/"
echo ""
echo "  2. 服务器上解压"
echo "     tar -xzf $PACKAGE_NAME"
echo ""
echo "  3. 查看部署说明"
echo "     cat DEPLOY.txt"
echo ""

